<?php
include_once dirname(__FILE__).'/_config.php';

$id = empty($_GET['id']) ? 0 : intval($_GET['id']);

$oGoodscat = new Goodscat();
if($id > 0){
	$category_info = $oGoodscat->get_row($id, '*');
	$category_info['category_tree'] = $oGoodscat->get_category_tree_ul(0, $id);	// 取分类树
	$category_info['attr'] = @unserialize($category_info['attr']);	// 属性
	$category_info['spec'] = @unserialize($category_info['spec']);	// 规格
	if(!is_array($category_info['attr'])){
		$category_info['attr'] = array();
	}
	if(!is_array($category_info['spec'])){
		$category_info['spec'] = array();
	}
	
	// 关联品牌信息
	if(!empty($category_info['brand'])){
		$ids = explode(',', trim($category_info['brand'], ', '));
		$oBrand = new Brand();
		$category_info['brand_list'] = array();
		foreach($ids as $brandid){
			$category_info['brand_list'][$brandid] = $oBrand->get_row($brandid, '`id`,`name`,`logo`');
		}
		$category_info['brand_json'] = json_encode($category_info['brand_list']);
	}
}else{
	$category_info = array(
		'pid'			=> empty($_GET['pid']) ? 0 : $_GET['pid'],
		'show'			=> 1,
		'rank'			=> 1000,
		'attr'			=> array(),
		'spec'			=> array(),
	);
	$category_info['category_tree'] = $oGoodscat->get_category_tree_ul($category_info['pid']);	// 取分类树
}

$category_info['show'] = make_radio_group('show', $show_arr, $category_info['show']);

// 属性/规格
$prototypes = Prototype::get_prototypes();
unset($prototypes['id_name_list']);
$vars['attrs'] = array();
$vars['specs'] = array();

$_attr_obj = array();
foreach ($category_info['attr'] as $prototypeid => $row) {
	$attr = $prototypes[$prototypeid];
	$attr['prompt'] = $row['prompt'];
	$attr['regexp'] = $row['regexp'];
	if(is_array($attr['value'])){
		$attr['input'] = '<select name="attr_input['.$prototypeid.']" class="select">'.(make_options($attr['value'], true, $row['default'])).'</select>';
	}else{
		$attr['input'] = '<input type="text" name="attr_input['.$prototypeid.']" value="'.(trim($row['default'])).'" class="input" style="width:160px;" />';
	}
	$vars['attrs'][$prototypeid] = $attr;
	
	$_attr_obj[$prototypeid] = array('id' => $attr['id'], 'name' => $attr['name']);
}
$category_info['attr_prototype_json'] = json_encode($_attr_obj);
$category_info['attr_count'] = count($_attr_obj);

$_spec_obj = array();
foreach ($category_info['spec'] as $prototypeid => $row) {
	$spec = $prototypes[$prototypeid];
	$spec['prompt'] = $row['prompt'];
	$spec['regexp'] = $row['regexp'];
	$spec['multi'] = $row['multi'];
	$spec['empty'] = $row['empty'];
	$row['item'] = isset($row['item']) ? $row['item'] : '';
	if(is_array($spec['value'])){
		$spec['input'] = make_checkbox_group("spec_item[{$prototypeid}]", $spec['value'], $row['item'], true);
	}else{
		$spec['input'] = '<input type="text" name="spec_item['.$prototypeid.']" value="'.(trim($row['item'])).'" class="input" style="width:160px;" />';
	}
	$vars['specs'][$prototypeid] = $spec;
	
	$_spec_obj[$prototypeid] = array('id' => $spec['id'], 'name' => $spec['name']);
	
}
$category_info['spec_prototype_json'] = json_encode($_spec_obj);
$category_info['spec_count'] = count($_spec_obj);

/*
foreach($prototypes as $prototypeid => $prototype_info){
	// 属性
	$attr = $prototype_info;
	if(array_key_exists($prototypeid, $category_info['attr'])){
		$attr['checked'] = ' checked';
		$value = $category_info['attr'][$prototypeid]['default'];
		$attr['prompt'] = $category_info['attr'][$prototypeid]['prompt'];
		$attr['regexp'] = $category_info['attr'][$prototypeid]['regexp'];
	}else{
		$attr['checked'] = '';
		if(is_array($attr['value'])){
			$value = '';
		}else{
			$value = $attr['value'];
		}
	}
	if(is_array($attr['value'])){
		$attr['input'] = '<select name="attr_input['.$prototypeid.']" class="select" style="width:160px;">'.(make_options($attr['value'], true, $value)).'</select>';
	}else{
		$attr['input'] = '<input type="text" name="attr_input['.$prototypeid.']" value="'.(trim($value)).'" class="input" />';
	}
	$vars['attrs'][$prototypeid] = $attr;
	
	// 规格
	$spec = $prototype_info;
	$spec['checked_empty'] = '';
	if(array_key_exists($prototypeid, $category_info['spec'])){
		$spec['checked'] = ' checked';
		$value = $category_info['spec'][$prototypeid]['default'];
		$spec['prompt'] = $category_info['spec'][$prototypeid]['prompt'];
		$spec['regexp'] = $category_info['spec'][$prototypeid]['regexp'];
		if($category_info['spec'][$prototypeid]['empty'] == 1){
			$spec['checked_empty'] = ' checked';
		}
	}else{
		$spec['checked'] = '';
		if(is_array($spec['value'])){
			$value = '';
		}else{
			$value = $spec['value'];
		}
	}
	if(is_array($spec['value'])){
		$spec['input'] = '<select name="spec_input['.$prototypeid.']" class="select" style="width:160px;">'.(make_options($spec['value'], true, $value)).'</select>';
	}else{
		$spec['input'] = '<input type="text" name="spec_input['.$prototypeid.']" value="'.(trim($value)).'" class="input" />';
	}
	$vars['specs'][$prototypeid] = $spec;
}
*/

if($category_info['pid'] > 0){
	$parent_category = $oGoodscat->get_row($category_info['pid'], 'id,name');
	$category_info['parent_name'] = $parent_category['name'];
}else{
	$category_info['parent_name'] = $__l['root'];
}

$vars['info'] = $category_info;


?>